package com.example.demo.services.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.BoardMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Board;
import com.example.demo.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service//加入到Spring
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public void create(Board board) {
        // 非空校验
        if (board == null) {
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 设备初值
        board.setArticleCount(0); // 帖子数量
        board.setState((byte) 0); // 状态
        board.setDeleteState((byte) 0); // 是否删除
        Date date = new Date();
        board.setCreateTime(date); // 创建时间
        board.setUpdateTime(date); // 更新时间
        int row = boardMapper.insertSelective(board);
        if (row != 1) {
            // 记录日志
            log.info(ResultCode.FAILED_CREATE.toString() + "版块名 = " + board.getName());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        log.info("新增版块成功. board = " + board.toString());
    }

    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if(num == null || num <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        List<Board> boards = boardMapper.selectByNum(num);
        //返回结果
        return boards;
    }

    @Override
    public Board selectById(Long id) {
        //非空校验
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        Board board = boardMapper.selectByPrimaryKey(id);
        //返回结果
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空校验
        if(id == null || id<= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取用户信息
        Board board = selectById(id);
        if(board == null || board.getDeleteState() == 1){
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构造更新对象
        Board updateBoard = new Board();
        //用户Id
        updateBoard.setId(board.getId());
        //发帖数在原来的基础上+1
        updateBoard.setArticleCount(board.getArticleCount() +1);
        //更新时间
        updateBoard.setUpdateTime(new Date());

        //调用DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }
}
